//
//  main.m
//  05-homework
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Province.h"
#import "City.h"
#import "District.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    // 一、
    // 1.创建1个包含5个字符串元素的数组；
    // 2.用for循环倒序输出所有元素；
    // 3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    NSArray *arrayOfWord = @[@"kobe", @" is", @" a", @" great", @" player"];
    for (NSInteger i = arrayOfWord.count - 1; i >= 0; i--) {
        NSLog(@"输出的结果是:%@", arrayOfWord[i]);
    }
    NSMutableString *stringOfWord = [NSMutableString string];
    for (NSString *strOfCharacter in arrayOfWord) {
       stringOfWord = [stringOfWord stringByAppendingString:strOfCharacter];
    }
    NSLog(@"拼接后的结果是:%@", stringOfWord)
    ;
    
    // 二、
    // 1.创建1个包含5个person对象的数组(姓名用英文)；
        Person *person1 = [[Person alloc] initWithName:@"liuyang" age:20 sex:@"male"];
    Person *person2 = [[Person alloc] initWithName:@"shengli" age:24 sex:@"male"];
    Person *person3 = [[Person alloc] initWithName:@"xiening" age:22 sex:@"male"];
    Person *person4 = [[Person alloc] initWithName:@"zuoyue" age:23 sex:@"female"];
    Person *person5 = [[Person alloc] initWithName:@"yinjiaen" age:22 sex:@"female"];
    NSArray *arrOfPerson = @[person1, person2, person3, person4, person5];
    // 2.用for循环找到年龄最大的Person对象；
    NSSortDescriptor *sortByAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    NSMutableArray *arrOfSortedPerson = [NSMutableArray array];
    for (int i = 0; i < arrOfPerson.count; i++) {
        [arrOfSortedPerson addObject:arrOfPerson[i]];
        [arrOfSortedPerson sortUsingDescriptors:@[sortByAge]];
    }
    NSLog(@"最大年龄的人是:%@", [arrOfSortedPerson lastObject]);
    // 3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    for (Person *person in arrOfPerson) {
        if (person.age >= 23) {
           person.name = [person.name stringByAppendingString:@"-brother"];
        }
            NSLog(@"判断之后的结果是:%@", person);
    }
    // 三、
    // 1.使用第二题目中生成的数组；
    // 2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
    arrOfPerson = [arrOfPerson sortedArrayUsingSelector:@selector(compareByName:)];
    NSLog(@"根据姓名排序后的结果:%@", arrOfPerson);
    arrOfPerson = [arrOfPerson sortedArrayUsingSelector:@selector(compareByAge:)];
    NSLog(@"根据年龄排序后的结果是:%@", arrOfPerson);
    arrOfPerson = [arrOfPerson sortedArrayUsingSelector:@selector(compareBySex:)];
    NSLog(@"根据性别排序的结果是;%@", arrOfPerson);
    // 3.使用NSSortDescriptor，按姓名进行排序。
    NSSortDescriptor *sortDesByAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    NSSortDescriptor *sortDesByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    arrOfPerson = [arrOfPerson sortedArrayUsingDescriptors:@[sortDesByAge,sortDesByName]];
    NSLog(@"通过姓名排序后的结果是:%@", arrOfPerson);
    // 四、
    // 处理省市区文件中的area.txt
    // 将省市区进行分割处理, 省里包含市, 市里包含区
    // 1. 先用数组字典表示
    // 2. 将数组字典的处理方式转换成数组模型(Class)
    NSString *path = @"/Users/dllo/Desktop/临时文件/Homework-05/area.txt";
    NSString *area = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfArea = [area componentsSeparatedByString:@"\n"];
    // 创建一个所有省份的数组
    NSMutableArray *provinceList = [NSMutableArray array];
    //创建NSCharacterSet,将area文件中得空格和数字全部去掉
    NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" 0123456789"];
    for (NSString *area in arrOfArea) {
        // 根据第一个字符是否为空格 确定是否为省份,*** !为取反 ***
        // 进入第一个if判断就会生成一个  *** areaDic ***
        if (![area hasPrefix:@" "]) {
        // 清除数字和空格
            NSString *provinceName = [area stringByTrimmingCharactersInSet:cSet];
            NSMutableArray *cityList = [NSMutableArray array];
            // 在字典中创建两个键值对,其中在第二个键值对的value为一个数组
            NSDictionary *areaDic = [NSDictionary dictionaryWithObjectsAndKeys:provinceName, @"provinceName", cityList, @"citylist",nil];
            [provinceList addObject:areaDic];
        }
        // 根据判断确定 市名
        // 进入第二个if判断就会生成 *** cityDic ***
        if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            NSString *cityName = [area stringByTrimmingCharactersInSet:cSet];
            // 创建该数组用来存放district, 其元素为字符串;
            NSMutableArray *districtNameArr = [NSMutableArray array];
            // 创建字典, 放到大数组中字典中的第二个键值的数组中
            NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:cityName, @"cityName", districtNameArr, @"districtName", nil];
            [[[provinceList lastObject] objectForKey:@"citylist"] addObject:cityDic];
        }
        if ([area hasPrefix:@"    "]) {
            NSString *districtName = [area stringByTrimmingCharactersInSet:cSet];
            [[[[[provinceList lastObject] objectForKey:@"citylist"] lastObject] objectForKey:@"districtName"] addObject:districtName];
        }
    }
    
    // 类方法
    // 建立Province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allProArray) {
        
        if (![area hasPrefix:@""]) {
            Province *pro = [[Province alloc] initWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City districtWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            District *district = [District districtWithName:area];
            [city.array addObject:district];
        }
    }
        // 最终形式
//     NSArray * arr = @[@{@"provinceName": @"北京", @"cityName": [@{@"cityName": @"北京市", @"distrctName": [@"西城区, @"东城区", @"崇文区", @"玄武区", @"朝阳区", @"丰台区"]}]}];
    // 五、拓展
    // 上述作业做完, 处理Location1.txt
    // 要求同第四题。自己寻找规律
    NSString *pathOfLocation = @"/Users/dllo/Desktop/临时文件/Homework-05/Location1.txt";
    NSString *stringOfLocation = [NSString stringWithContentsOfFile:pathOfLocation encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfLocation = [stringOfLocation componentsSeparatedByString:@","];
    // 对数组进行重新排序
    NSSortDescriptor *sortDes = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    // 创建排序的后的数组
    NSMutableArray *newSortedLocationArr = [NSMutableArray array];
    NSMutableArray *newSortedLocationArrTwo = [NSMutableArray array];
    for (NSString *str in arrOfLocation) {
        NSArray *arrOfSeg = [str componentsSeparatedByString:@"|"];
        NSString *newSortdeStr = [[arrOfSeg lastObject] stringByAppendingString:[arrOfSeg firstObject]];
        [newSortedLocationArr addObject:newSortdeStr];
    }
    [newSortedLocationArr sortUsingDescriptors:@[sortDes]];
    for (NSString *newSortedStr in newSortedLocationArr) {
        NSString *seporatoredOneSegment = [newSortedStr substringToIndex:6];
        NSString *seporatoredTowSegment = [newSortedStr substringFromIndex:6];
        NSString *newSortedStrOfLocation = [[seporatoredTowSegment stringByAppendingString:@"|"] stringByAppendingString:seporatoredOneSegment];
        [newSortedLocationArrTwo addObject:newSortedStrOfLocation];
    }
    // 创建一个数组provinceList
    NSMutableArray *arrOfProvice = [NSMutableArray array];
    for (NSString *strOfLocation in newSortedLocationArrTwo) {
        NSArray *arrOfSegmentation = [strOfLocation componentsSeparatedByString:@"|"];
        if ([[[arrOfSegmentation lastObject] substringFromIndex:2] isEqualToString:@"0000"]) {
            NSMutableArray *cityArr = [NSMutableArray array];
            NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:[arrOfSegmentation firstObject], @"provinceName", cityArr, @"cityList", nil];
            [arrOfProvice addObject:cityDic];
        }
        if ([[[arrOfSegmentation lastObject] substringFromIndex:4] isEqualToString:@"00"] && ![[[arrOfSegmentation lastObject] substringWithRange:NSMakeRange(2, 2)] isEqualToString:@"00"]) {
            NSMutableArray *districArr = [NSMutableArray array];
            NSDictionary *districtDic = [NSDictionary dictionaryWithObjectsAndKeys:[arrOfSegmentation firstObject],@"cityName",districArr, @"districtName", nil];
            [[[arrOfProvice lastObject] objectForKey:@"cityList"] addObject:districtDic];
        }
        if (![[[arrOfSegmentation lastObject] substringFromIndex:4] isEqualToString:@"00"]) {
            // 注意: 写代码尽量分开写
            [[[[[arrOfProvice lastObject] objectForKey:@"cityList"] lastObject] objectForKey:@"districtName"] addObject:[arrOfSegmentation firstObject]];
        }
    }
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in arrOfLocation) {
        //  根据 | 切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    // 对 类数组 排序
    NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sort]];
    NSMutableArray *fianlClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province tempCityWithTempcity:tempCity];
            [fianlClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [fianlClassArray lastObject];
            City *city = [City tempCityWithTempcity:tempCity];
            [pro.array addObject:city];
        } else {
            Province *pro = [fianlClassArray lastObject];
            City *city = [pro.array lastObject];
            District *district = [District tempCityWithTempcity:tempCity];
            [city.array addObject:district];
        }
    }
    
    return 0;
    
}
